package com.junyang.showdata.controller;

import com.junyang.showdata.common.CityDataCountDemo;
import com.junyang.showdata.common.RiskDataByYearAndDay;
import com.junyang.showdata.common.RiskDemo;
import com.junyang.showdata.common.RiskLevelDemo;
import com.junyang.showdata.pojo.CityCountWithName;
import com.junyang.showdata.pojo.CityData;
import com.junyang.showdata.service.CityDataService;

import com.junyang.showdata.utils.Result;
import com.junyang.showdata.utils.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import java.util.*;

/**
 * @param
 * @Author fusijia
 * @Date 2025/1/13 16:02
 * @notes city_data表中的数据操作
 */
@RestController
@RequestMapping("/cityData")
@CrossOrigin
@Slf4j
public class CityDataController {

    @Autowired
    private CityDataService cityDataService;

    private Integer vTime=0;

    /**
    *@Author：fusijia
    *@Date：2025/1/13 16:02
    *@return：返回需要地市的数据信息
    *@param: 地市标识
    */
    @PostMapping("/getListById")
    public Result<List<CityData>> getListById(Integer id) {
        if (id == null || id <= 0) {
            return Result.fail(ResultCodeEnum.ILLEGAL_REQUEST.getResultCode(), ResultCodeEnum.ILLEGAL_REQUEST.getResultMsg());
        }
        List cityDataList = cityDataService.getListById(id);
        return new Result<>().success(cityDataList);
    }

    /**
     *@Author：zjn
     *@Date：2025/1/14 21:02
     *@return：返回需要成功率的数据信息
     *@param: 地市标识
     */
    @PostMapping("/getCompleteList")
    public Result<List<CityData>> getCompleteList() throws Exception {
        try {
            List CompleteList = cityDataService.getCompleteList();
            return new Result<>().success(CompleteList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(ResultCodeEnum.DATA_ERROR.getResultCode(), ResultCodeEnum.DATA_ERROR.getResultMsg());
        }
    }

    /**
     * @notes 根据风险等级查所有数据和完成数据
     *@Author：fusijia
     *@Date：2025/1/14 16:02
     *@return：返回该风险等级下的总数和完成数
     *@param: 风险等级
     */
    @PostMapping("/getRiskCount")
    public Result<RiskDemo> getRiskCount(Integer riskLevel) {
        if (riskLevel == null || riskLevel < 0 || riskLevel > 3) {
            return Result.fail(ResultCodeEnum.ILLEGAL_REQUEST.getResultCode(), ResultCodeEnum.ILLEGAL_REQUEST.getResultMsg());
        }
        RiskDemo riskDemo = cityDataService.getRiskCount(riskLevel);
        return new Result<>().success(riskDemo);
    }

    /**
     * @notes 获取各等级风险数据及总数
     *@Author：fusijia
     *@Date：2025/1/14 16:02
     *@return：RiskLevelDemo
     *@param:
     */
    @PostMapping("/getRiskLevelCount")
    public Result<RiskLevelDemo> getRiskLevelCount() {
        RiskLevelDemo riskLevelDemo = cityDataService.getRiskLevelCount();
        return new Result<>().success(riskLevelDemo);
    }

    /**
     * @notes 获取年，月，周，日时间段内的数据数量
     *@Author：fusijia
     *@Date：2025/1/14 16:02
     *@return：数据数量RiskDataByYearAndDay
     *@param:
     */
    @PostMapping("/getByYearAndDay")
    public Result<RiskDataByYearAndDay> getByYearAndDay() {
        RiskDataByYearAndDay riskDataByYearAndDay = cityDataService.getByYearAndDay();
        return new Result<>().success(riskDataByYearAndDay);
    }

    /**
     *@notes:根据不同地区等级获取数据数量
     *@Author：fusijia
     *@Date：2025/1/15 10:02
     *@return：CityDataCountDemo 各等级地区数量数据多少
     *@param:
     */
    @PostMapping("/getCityDataCountByLevel")
    public Result<CityDataCountDemo> getCityDataCountByLevel() {
        CityDataCountDemo cityDataCountDemo = cityDataService.getCityDataCountByLevel();
        return new Result<>().success(cityDataCountDemo);
    }

    /**
     *@Author：zjn
     *@Date：2025/1/14 21:02
     *@return：返回需要成功率的数据信息,0年，1月，2周
     *@param: 地市标识
     */
    @PostMapping("/getRiskBytime")
    public Result<List<CityData>> getRiskByTime(Integer vTime) throws Exception{
        try {
                List<CityData> RiskByTimeList = cityDataService.getRiskByTime(vTime);
                return new Result<>().success(RiskByTimeList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(ResultCodeEnum.ILLEGAL_REQUEST.getResultCode(), ResultCodeEnum.ILLEGAL_REQUEST.getResultMsg());
        }

    }



    /**
     *@notes:根据地区等级联合查询数据数量并排序和返回对地区名称列表
     *@Author：fusijia
     *@Date：2025/1/15 11:02
     *@return：CityDataCountDemo 各等级地区数量数据及名称列表
     * @param
     */
    @PostMapping("/getCityDataMapByLevel")
    public Result<List<CityCountWithName>> getCityDataMapByLevel(Integer level) {
        if (level == null || level <= 0 || level > 3) {
            return Result.fail(ResultCodeEnum.ILLEGAL_REQUEST.getResultCode(), ResultCodeEnum.ILLEGAL_REQUEST.getResultMsg());
        }
        List<CityCountWithName> cityDataCountWithName = cityDataService.getCityDataMapByLevel(level);
        return new Result<>().success(cityDataCountWithName);
    }

}
